package org.unt.encoding;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class CharacterEncoding {

    public static void main(String[] args) throws Exception {

        // System.out.println(detectEncoding("a3.txt"));
        // writeFile("a1.txt", "�".getBytes());
        // writeFile("a2.txt", "�".getBytes("UTF-8"));
        // writeFile2("a3.txt", new byte[] { 3, 2 });
         writeFile3("a4.txt", "a�");

    }

    static void writeFile3(String file, String str) throws Exception {
        FileOutputStream fos = new FileOutputStream("C:/X86/" + file);
        OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
        osw.write(str);
        osw.close();
    }

    static void writeFile2(String file, byte[] bytes) throws Exception {
        FileOutputStream fos = new FileOutputStream("C:/X86/" + file);
        OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
        for (int i = 0; i < bytes.length; i++) {
            osw.write(bytes[i]);
        }
        osw.close();
    }

    static String detectEncoding(String file) throws Exception {
        File in = new File("C:/X86/" + file);
        InputStreamReader r = new InputStreamReader(new FileInputStream(in));
        return r.getEncoding();
    }

    static void writeFile1(String file, byte[] bytes) throws Exception {
        FileWriter fw = new FileWriter(new File("C:/X86/" + file));
        for (int i = 0; i < bytes.length; i++) {
            fw.write(bytes[i]);
        }
        fw.close();
    }
}
